#!/bin/bash
# queues a message in the sms sending thingy

SENDTOLIST=sendto
SENTTOLIST=sentto
FAILSENDLIST=failmsg
SENDMSG=sendmsg
MSGQUEUE=sendq

# only queue messages we are already done sending
while true; do
	if [ -f "$SENDTOLIST" ] && [ -f "$SENTTOLIST" ] && diff "$SENDTOLIST" <(cut -f 1 -d ' ' "$SENTTOLIST") -N > /dev/null && ls "${MSGQUEUE}"* > /dev/null 2>/dev/null ; then

		# put the queue messages to a variable
		count=0
		for x in "$MSGQUEUE"*; do
			QUEUE[$count]="$x"
			count=$((count+1))
		done

		# attempt to queue the first message
		datenow=`date +%Y%m%d%H%M%S`
		echo "$datenow: Queueing a new message"
		mv -v "$SENDMSG" "${SENDMSG}-${datenow}"
		mv -v "$FAILSENDLIST" "${FAILSENDLIST}-${datenow}"
		mv -v "${QUEUE[0]}" "$SENDMSG"
		mv -v "$SENTTOLIST" "${SENTTOLIST}-${datenow}"

		unset QUEUE
	fi

	sleep 60
done
